﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TPOJ.Model;
using TPOJ.BLL;

namespace TPOJ.Web.Controllers
{
    public class ForumController : Controller
    {
        //
        // GET: /Forum/Index/1000

        public ActionResult Index(int id)
        {
            var messages = ForumDataProxy.GetForumMessagesByProblemId(id);
            ViewData["pid"] = id;
            ViewData.Model = messages.Where(m => m.ParentId == null);
            return View();
        }

        //
        // GET: /Forum/Details/5

        public ActionResult Details(int id)
        {
            var message = ForumDataProxy.GetForumMessage(id);
            int problemId = message.ProblemId ?? 1000; //Need Change here.
            var followMessages = ForumDataProxy.GetForumMessagesByProblemId(problemId);
            ViewData["FollowMessages"] = followMessages.Where(m => m.ParentId == id);
            ViewData.Model = message;
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Details(ForumMessage message)
        {
            try
            {
                ForumBO forumBO = new ForumBO();
                AccountBO accountBO = new AccountBO();
                message.AccountId = accountBO.GetAccountByName(User.Identity.Name).UniqueId;
                message.DateTime = DateTime.Now;
                message.Defunct = false;
                message.ParentId = message.UniqueId;
                message.UniqueId = 0;

                forumBO.Create(message);

                return RedirectToAction("Index", new { id = message.ProblemId });
            }
            catch
            {
                return RedirectToAction("Details", new { id = message.ParentId });
            }
        }

        //
        // GET: /Forum/Create

        public ActionResult Create(int? id)
        {
            int pid = id ?? 1000;
            ViewData.Model = new ForumMessage { ProblemId = pid };
            return View();
        } 

        //
        // POST: /Forum/Create
        
        [Authorize]
        [HttpPost]
        public ActionResult Create(ForumMessage message)
        {
            try
            {
                ForumBO forumBO = new ForumBO();
                AccountBO accountBO = new AccountBO();
                message.AccountId = accountBO.GetAccountByName(User.Identity.Name).UniqueId;
                message.DateTime = DateTime.Now;
                message.ParentId = null;
                message.Defunct = false;
                forumBO.Create(message);

                return RedirectToAction("Index", new { id = message.ProblemId });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Forum/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Forum/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
